<?php

namespace app\admin\controller\system;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\logic\NodeLogic;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="节点管理",
 *     parent="admin/system.setting",
 *     is_menu=0
 * )
 */
Class Node extends Admin
{
    protected $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\system\Node();
    }

    /**
     * @NodeAnnotation(
     *     title="节点列表",
     *     parent="admin/system.node",
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['title',"like","%$keywords%"]);


        $nodeData = $this->model->where($where)->paginate();

        $nodeList = $nodeData->toArray();

        $newNodeList = [
            "data" => (new NodeLogic())->processNode($nodeList['data']),
            "total" => $nodeList['total']
        ];

        $this->assign([
            "nodeList" => $newNodeList,
            "nodePate" => $nodeData->render(),
            "search"   => [
                "keywords" => $keywords
            ]
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="导入节点",
     *     parent="admin/system.node",
     *     is_menu=0
     * )
     */
    public function addnode()
    {
        $apps = scan_dir(app_path() . '*', GLOB_ONLYDIR);
        $app = $this->request->param('app', '');

        array_unique($apps);
        unset($apps[array_search("common", $apps)]);
        unset($apps[array_search("http", $apps)]);
        sort($apps);

        if (empty($app)) {
            $app = $apps[0];
        }

        if (!in_array($app, $apps)) {
            $this->error('应用' . $app . '不存在!');
        }

        $newMenus = NodeLogic::importNode([$app]);

        $index = array_search($app, $apps);

        $nextIndex = $index + 1;

        $nextIndex = $nextIndex >= count($apps) ? 0 : $nextIndex;

        if ($nextIndex) {
            $this->assign("next_app", $apps[$nextIndex]);
        }

        $this->assign("app", $app);
        $this->assign("new_menus", $newMenus);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="更新节点",
     *     parent="admin/system.node",
     *     is_menu=0
     * )
     */
    public function refnode()
    {
        $apps = scan_dir(app_path() . '*', GLOB_ONLYDIR);
        $app = $this->request->param('app', '');

        array_unique($apps);
        unset($apps[array_search("common", $apps)]);
        unset($apps[array_search("http", $apps)]);
        sort($apps);

        if (empty($app)) {
            $app = $apps[0];
        }

        if (!in_array($app, $apps)) {
            $this->error('应用' . $app . '不存在!');
        }

        $newMenus = NodeLogic::updateNode([$app]);

        $index = array_search($app, $apps);

        $nextIndex = $index + 1;

        $nextIndex = $nextIndex >= count($apps) ? 0 : $nextIndex;

        if ($nextIndex) {
            $this->assign("next_app", $apps[$nextIndex]);
        }

        $this->assign("app", $app);
        $this->assign("new_menus", $newMenus);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="清除节点",
     *      parent="admin/system.node",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function clearExceptionNode()
    {
        $nodeList = $this->model->getNodeList()->toArray();

        $nodeList = (new NodeLogic())->processNode($nodeList);

        $result = $this->model->clearExceptionNode($nodeList);

        if ($result) {
            $this->success("清楚成功");
        } elseif (is_array($result) and empty($result)) {
            $this->error("暂无所需要清楚的节点");
        }else{
            $this->error("清楚失败");
        }
    }

    /**
     * @NodeAnnotation(
     *     title="菜单转换",
     *     parent="admin/system.node",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function ismenu(Request $request)
    {
        $id = $request->param("id", 0, "intval");

        $is_menu = $request->param("is_menu",0,"intval");

        $result = $this->model->isMenu($id,$is_menu);

        if ($result and $id) {

            $node = $this->model->findNode($id)->toArray();

            if($node && $is_menu==1)
            {
                unset($node['id']);
                $node['url_link'] = $node['name'];

                $parent_id = $request->param("parent_id",0,'intval');

                $node['parent_id'] = $parent_id;

                (new \app\admin\model\system\Menu())->addMenu($node);

            }else{

                (new \app\admin\model\system\Menu())->delMenu([
                    "name"=>$node['name']
                ]);

            }
            $this->success("更改成功");

        } else {

            $this->error("更改失败");

        }
    }


    /**
     * @NodeAnnotation(
     *     title="权限验证控制",
     *     parent="admin/system.node",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function auth(Request $request)
    {
        $id = $request->param("id", 0, "intval");

        $auth  = $request->param("auth",0,"intval");

        $result = $this->model->isAuth($id,$auth);

        if ($result and $id)
        {
            $this->success("更改成功");

        } else {
            $this->error("更改失败");

        }
    }

}